﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Krav_Player
{
    public class Krav_Player
    {

        private String Pcommand;
        private bool isOpen;
        
        private int volume;
        private int navigate;

        [DllImport("winmm.dll")]
        private static extern long mciSendString(string strCommand,
            StringBuilder strReturn, int iReturnLength,IntPtr hwndCallback);

        private static Krav_Player player;
        private int baseVolumn;
        private int trebleVolumn;
        private int leftVolumn;
        private int rightVolumn;
        private int masterVolumn;
      



        public Krav_Player()
        {
            navigate = 0;
            this.Bass = 100;
            this.LeftVolume = 100;
            this.volume = 50;
            this.MasterVolume = 100;
            this.RightVolume = 100;
            this.Treble = 100;
        }


        public static Krav_Player GetPlayer()
        {
            if (player == null)
                player = new Krav_Player();
            return player;
        }
        public void Close()
        {
            Pcommand = "close MediaFile";
            mciSendString(Pcommand, null, 0, IntPtr.Zero);
            isOpen = false;
        }

        public void Open(string FileName)
        {
            Pcommand = "open \"" + FileName + "\" type mpegvideo alias MediaFile";
            mciSendString(Pcommand, null, 0, IntPtr.Zero);
            isOpen = true;
        }

        //
        //Длинна файла, секунды
        //
        public int Length
        {
            get
            {
                if (isOpen)
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder(128);
                    mciSendString("status MediaFile length", sb, 128, IntPtr.Zero);
                    if (sb.Length == 0) return 0;
                    return Convert.ToInt32(sb.ToString()) / 1000;
                }
                else return 0;
            }
        }

        public int Volume
        {
            get
            {
                return this.volume;
            }
            set
            {
                this.volume = value;
                mciSendString("setaudio MediaFile volume to " + value * 10, null, 0, IntPtr.Zero);
            }
        }


        /// <summary>
        /// Воспроизведение уже открытого файла по кругу или нет
        /// </summary>
        public void Play(bool loop)
        {
            if (isOpen)
            {
                Pcommand = "play MediaFile";
                if (loop)
                    Pcommand += " REPEAT";
                mciSendString(Pcommand, null, 0, IntPtr.Zero);
                this.Bass = this.Bass;
                this.LeftVolume = this.LeftVolume;
                this.MasterVolume = this.MasterVolume;
                this.RightVolume = this.RightVolume;
                this.Treble = this.Treble;
            }
        }


        /// <summary>
        /// Воспроизведение указаного файла
        /// </summary>
        public void Play(string FileName)
        {
            if (isOpen == true)
            {
                Close();
            }
            Open(FileName);
            Play(false);
        }

        /// <summary>
        /// Установка паузы
        /// </summary>
        public void Pause()
        {
            Pcommand = "pause MediaFile";
            mciSendString(Pcommand, null, 0, IntPtr.Zero);
           
        }

        /// <summary>
        /// Получение текущего статуса
        /// </summary>
        public String Status()
        {
            int i = 128;
            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(i);
            mciSendString("status MediaFile mode", stringBuilder, i, IntPtr.Zero);
            return stringBuilder.ToString();
        }

        public int Navigate
        {
            get
            {
                if (!isOpen) return 1;
                System.Text.StringBuilder sb = new System.Text.StringBuilder(128);
                mciSendString("status MediaFile position", sb, 128, IntPtr.Zero);
                return Convert.ToInt32(sb.ToString()) / 1000;
            }
            set
            {
                this.navigate = value;
                mciSendString("seek MediaFile to " + value * 1000, null, 0, IntPtr.Zero);
                mciSendString("play MediaFile", null, 0, IntPtr.Zero);
            }
        }

        /// <summary>
        /// Получение/Установка громкости левого динамика
        /// </summary>
        public int LeftVolume
        {
            get
            {
                return leftVolumn;
            }
            set
            {
                mciSendString(string.Concat("setaudio MediaFile left volume to ", value), null, 0, IntPtr.Zero);
                leftVolumn = value;
            }
        }

        /// <summary>
        /// Получение/Установка громкости правого динамика
        /// </summary>
        public int RightVolume
        {
            get
            {
                return rightVolumn;
            }
            set
            {
                mciSendString(string.Concat("setaudio MediaFile right volume to ", value), null, 0, IntPtr.Zero);
                rightVolumn = value;
            }
        }

        /// <summary>
        /// Получение/Установка общей громкости воспроизведения
        /// </summary>
        public int MasterVolume
        {
            get
            {
                return masterVolumn;
            }
            set
            {
                mciSendString(string.Concat("setaudio MediaFile volume to ", value), null, 0, IntPtr.Zero);
                masterVolumn = value;
            }
        }

        /// <summary>
        /// Получение/Установка громкости басов
        /// </summary>
        public int Bass
        {
            get
            {
                return baseVolumn;
            }
            set
            {
                mciSendString(string.Concat("setaudio MediaFile bass to ", value), null, 0, IntPtr.Zero);
                baseVolumn = value;
            }
        }

        /// <summary>
        /// Устанавливает/получения звукового высокочастотного уровеня.
        /// </summary>
        public int Treble
        {
            get
            {
                return trebleVolumn;
            }
            set
            {
                mciSendString(string.Concat("setaudio MediaFile treble to ", value), null, 0, IntPtr.Zero);
                trebleVolumn = value;
            }
        }

        /// <summary>
        /// Проверка установленна ли пауза
        /// </summary>
        public bool IsPaused()
        {
            return Pcommand == "pause MediaFile";
        }
        /// <summary>
        /// Проверка происходит ли проигрывание
        /// </summary>
        public bool IsPlaying()
        {
         return Status() == "playing";
        }
        /// <summary>
        /// Проверка Открыт ли какой либо файл
        /// </summary>
        public bool IsOpen()
        {
            return isOpen;
        }

    }
}
